import type {Survey} from "@/store/modules/survey.store.ts";

export const getSurvey = (id: string): Survey | null => {
    const surveys = localStorage.getItem('surveys')
    if (!surveys) return null

    const parsed = JSON.parse(surveys) as Survey[]
    return parsed.find(s => s.id === id) || null
}

export const saveSurvey = (survey: Survey) => {
    const surveys = localStorage.getItem('surveys')
    let parsed: Survey[] = []

    if (surveys) {
        parsed = JSON.parse(surveys)
        const index = parsed.findIndex(s => s.id === survey.id)

        if (index !== -1) {
            parsed[index] = survey
        } else {
            parsed.push(survey)
        }
    } else {
        parsed = [survey]
    }

    localStorage.setItem('surveys', JSON.stringify(parsed))
}